ActiveAdmin.register Appointment do
  config.batch_actions = false
  actions :index

  filter :company_name, as: :string, label: '集团'
  filter :store_name, as: :string, label: '门店'
  filter :course_name, as: :string, label: '课程'
  filter :store_city, as: :string, label: '城市'
  filter :member_mobile, as: :string, label: '会员手机号'
  filter :class_start_time, label: '上课时间'
  filter :order_total, as: :numeric, label: '订单金额'
  filter :order_source, as: :string, label: '订单来源'
  filter :order_pay_amount, as: :numeric, label: '支付金额'

  index do
    column :order_no
    column :quantity
    column :member_name
    column :member_mobile
    column :class_start_time
    column :coach_name
    column :company_name
    column :store_name
    column :status_i18n
    column :is_member
    column :source
    column :created_at
  end

  controller do
    def scoped_collection
      Appointment.joins(:order, :company, :store, :coach, :member, :course).select(
          :timetable_id,
          "courses.id as course_id",
          "CONCAT(courses.en_name,'|',courses.name) as course_name",
          "CASE WHEN courses.usage = 0 THEN '免费课' ELSE '收费课' END as usage",
          'member_card_id != 0 as is_member',
          'members.name as member_name',
          'members.mobile as member_mobile',
          'orders.order_no',
          'orders.pay_type',
          'appointments.class_start_time',
          'coaches.name as coach_name',
          'companies.name as company_name',
          'stores.name as store_name',
          :quantity,
          :status,
          "orders.source as source",
          :created_at
      )
    end

    def index
      if params[:format].eql?("csv") || params[:format].eql?("xls")
        sql = Appointment.ransack(params[:q]).result.to_sql
        filter = sql.split("WHERE")[1]
        csv = CSV.generate(headers: true) do |row|
          row << %w{课表编号 课程编号 课程名 课程类型 订单号 名额 课程时间 教练 集团 门店 状态 会员 会员名 会员手机号 来源 预约时间}
          Appointment.joins(:order, :company, :store, :coach, :member, :course).where(filter).pluck(
              Arel.sql(
                  "appointments.timetable_id, courses.id as course_id,
                CASE appointments.course_en_name WHEN appointments.course_cn_name THEN appointments.course_cn_name ELSE CONCAT(appointments.course_en_name, '|', appointments.course_cn_name) END,
                CASE courses.usage WHEN 0 THEN '免费课' ELSE '收费课' END AS course_usage,orders.order_no,
                appointments.quantity AS quantity,appointments.class_start_time,coaches.name,
                companies.name AS company_name,stores.name,
                CASE appointments.status WHEN 0 THEN '已取消' WHEN 1 THEN '已预约' WHEN 2 THEN '已缺席' WHEN 3 THEN '已签到' WHEN 4 THEN '已停课' WHEN 5 THEN '已评论' END,
                appointments.member_card_id != 0,
                members.name,members.mobile,orders.source,appointments.created_at"
              )
          ).each {|data| row << data}
        end
        send_data("\uFEFF" + csv, type: Mime[:csv], disposition: "attachment; filename=预约信息-#{Time.current.to_i}.csv")
      else
        index!
      end
    end
  end
end
